﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using QS.Logic.Objects;
using Microsoft.Xna.Framework;

namespace QS.Game.Camera
{
    class QSmoothThirdPersonCamera : QCamera
    {
        private QSpaceShip spaceShip;
        private Vector3 relPosition;

        public QSmoothThirdPersonCamera(GraphicsDevice device, QSpaceShip spaceShip)
            : base(device)
        {
            this.spaceShip = spaceShip;
            m_ProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45), 800f / 480f, 0.01f, 1000f);
            relPosition = new Vector3(0, 1, 4);
            updateLookAt();
        }

        public override void Update(GameTime time)
        {
            base.Update(time);
            updateLookAt();
            UpdatePosition(time);
        }

        private void updateLookAt()
        {
            m_ViewMatrix = Matrix.CreateLookAt( Position , this.spaceShip.Position + new Vector3(0, 1, 0), Vector3.Up);          
        }

        private void UpdatePosition(GameTime time)
        {
            Vector3 targetPos = this.spaceShip.Position + Vector3.Transform(relPosition, this.spaceShip.Orientation);
            Vector3 deltaPos = targetPos - Position;
            float dist = deltaPos.Length();
            Position += deltaPos * 0.1f;
        }
    }
}
